﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Runtime;

using Qiang.Topology.Geometries;
using Qiang.QMath;

namespace Qiang.Data
{
    [Serializable]
    public class VectorSet : Dataset,IVectorSet
    {

        #region 初始化

        /// <summary>
        /// 自定义矢量数据集
        /// </summary>
        public VectorSet()
            :base()
        {
            Configure();
        }



        void Configure()
        {
            ShapeList = new ShapeList(this);
            _polygonBuilder = new PolygonBuilder(this);
        }


        #endregion
        

        #region 事件

        /// <summary>
        /// 图形添加事件
        /// </summary>
        public event EventHandler<ShapeEventArgs> ShapeAdded;

        /// <summary>
        /// 图形移除事件
        /// </summary>
        public event EventHandler<ShapeEventArgs> ShapeRemoved;

        /// <summary>
        /// 图形改变事件
        /// </summary>
        public event EventHandler<ShapeEventArgs> ShapeChanged;

        #endregion


        #region ShapeList事件相关

        /// <summary>
        /// 订阅图形集合事件
        /// </summary>
        /// <param name="shapeLst"></param>
        void HandleShapeListEvent(IShapeList shapeLst)
        {
            if (shapeLst == null) return;
            shapeLst.ShapeAdded += OnShapeAdded;
            shapeLst.ShapeRemoved += OnShapeRemoved;
        }

        /// <summary>
        /// 移除图形集合事件
        /// </summary>
        /// <param name="shapeLst"></param>
        void RemoveShapeListEvent(IShapeList shapeLst)
        {
            if (shapeLst == null) return;
            shapeLst.ShapeAdded -= OnShapeAdded;
            shapeLst.ShapeRemoved -= OnShapeRemoved;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Sender"></param>
        /// <param name="args"></param>
        void OnShapeAdded(Object Sender,ShapeEventArgs args)
        {
            if (ShapeAdded != null)
            {
                ShapeAdded(this, args);
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Sender"></param>
        /// <param name="args"></param>
        void OnShapeRemoved(Object Sender, ShapeEventArgs args)
        {
            
            if (ShapeRemoved != null)
            {
                ShapeRemoved(this, args);
            }
        }

        #endregion


        #region 字段

        /// <summary>
        /// 图形集合
        /// </summary>
        private IShapeList _shapeList;

        /// <summary>
        /// 发展表
        /// </summary>
        private DataTable _dataTable;

        /// <summary>
        /// 
        /// </summary>
        PolygonBuilder _polygonBuilder;

       

        #endregion


        # region 属性


        /// <summary>
        /// 图形集合
        /// </summary>
        public IShapeList ShapeList
        {
            get
            {
                return _shapeList;
            }
            set
            {
                if (_shapeList != null)
                {
                    RemoveShapeListEvent(_shapeList);
                }
                _shapeList = value;
                if (value != null)
                {
                    HandleShapeListEvent(value);
                }
            }
        }

        /// <summary>
        /// 属性表
        /// </summary>
        public virtual DataTable DataTable
        {
            get { return _dataTable; }
            set 
            {
                OnDataTableExcluded(_dataTable);
                _dataTable = value;
                OnDataTableIncluded(_dataTable);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public PolygonBuilder PolygonBuilder
        {
            get { return _polygonBuilder; }
            set { _polygonBuilder = value; }
        }

        # endregion
               

        #region 属性表方法

        /// <summary>
        /// 取消包含属性表。取消订阅属性表的事件
        /// </summary>
        /// <param name="dataTable"></param>
        private void OnDataTableExcluded(DataTable dataTable)
        {
            dataTable.RowDeleted -= DataTableRowDeleted;
        }

        /// <summary>
        /// 包含属性表。订阅属性表的事件
        /// </summary>
        /// <param name="dataTable"></param>
        private void OnDataTableIncluded(DataTable dataTable)
        {
            dataTable.RowDeleted -= DataTableRowDeleted;
        }

        /// <summary>
        /// 删除属性表行事件的实现方法。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataTableRowDeleted(object sender, DataRowChangeEventArgs e)
        { 
        }

        #endregion


        # region 继承的方法

        /// <summary>
        /// 获取数据集的范围
        /// </summary>
        /// <returns></returns>
        protected override IExtent ComputeExtent()
        {
            IExtent extent = new Extent();

            if (_shapeList==null || _shapeList.Count==0)
            {
                return new Extent(-180, -90, 180, 90);
            }

            foreach (IShape shp in _shapeList)
            {
                extent = extent.Union(shp.Extent);
            }
                       

            return extent;
        }


        # endregion
        

        # region 添加图形

        /// <summary>
        /// 添加图形
        /// </summary>
        /// <param name="shp"></param>
        public void AddShape(IShape shp)
        {
            _shapeList.Add(shp);
        }



        #endregion

        
        #region 删除图形

        /// <summary>
        /// 删除图形
        /// </summary>
        /// <param name="shp"></param>
        public bool RemoveShape(IShape shp)
        {
            return _shapeList.Remove(shp);
        }

        /// <summary>
        /// 清空图形。
        /// </summary>
        public void Clear()
        { 
        }


        # endregion


        #region 构造多边形

        public void BuildPolygon()
        {
            _polygonBuilder.BuildPolygon();
        }
        
        #endregion


        #region 释放资源

        /// <summary>
        /// 释放非托管资源
        /// </summary>
        /// <param name="IsDisposeUnManagedResource">是否释放非托管资源</param>
        protected override void Dispose(bool IsDisposeUnManagedResource)
        {
            if (IsDisposeUnManagedResource)
            {
                _shapeList = null;
                
            }
        }

        #endregion


    }
}
